<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../res/layui/css/layui.css">
</head>
<body>
<div class="layui-fluid">

    <fieldset class="layui-elem-field layui-field-title">
        <legend>频道添加</legend>
        <div class="layui-field-box">
            <div class="layui-col-md6">
                <form class="layui-form layui-form-pane" lay-filter="formMusic" id="formMusic">
                    <div class="layui-form-item">
                        <img class="layui-upload-img" id="ID-upload-demo-img"
                             style="width:92px;height: 92px;border: 1px solid rgba(0,0,0,0.1)">
                    </div>

                    <!--表单控件项：文件上传（封面图）-->
                    <div class="layui-form-item">
                        <button class="layui-btn layui-btn-normal" type="button" id="btnChannel">
                            <i class="layui-icon layui-icon-upload"></i>
                            选择封面图
                        </button>
                        <input type="hidden" name="img" id="img">
                    </div>

                    <!--表单控件项：频道分类名-->
                    <div class="layui-form-item">
                        <label class="layui-form-label">频道分类名:</label>
                        <div class="layui-input-block">
                            <input class="layui-input" type="text" name="name" id="musicname" lay-verify="required"
                                   lay-verType="tips" lay-reqText="请输入频道分类名">
                        </div>
                    </div>

                    <!--表单控件项：简介-->
                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label">简介:</label>
                        <div class="layui-input-block">
                            <textarea name="summary" class="layui-textarea" placeholder="请填写频道分类简介"></textarea>
                        </div>
                    </div>

                    <!-- 提交按钮 -->
                    <div class="layui-form-item" style="text-align: right">
                        <button type="button" class="layui-btn layui-btn-warm" lay-submit lay-filter="addChannel">确认添加
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </fieldset>
</div>
<script src="../res/layui/layui.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
    layui.use(['form', 'upload'], function () {
        var form = layui.form;
        var upload = layui.upload;

        // 文件上传组件渲染
        upload.render({
            elem: '#btnChannel' //绑定触发上传操作的元素对象（按钮）
            , url: "/category/upload"   //服务器接收文件的地址
            , field: "file"       //服务端接收文件的参数名(文件域字段名)
            , accept: "images"   //指定允许上传的文件类型
            , acceptMime: "image/*"    //打开文件选择框时，默认筛选的文件类型
            , done: function (resp) {
                console.log(resp)
                if (resp.code === 0) {
                    //上传成功
                    //将文件地址写入隐藏域
                    $("#img").val(resp.data);
                    $('#ID-upload-demo-img').attr('src', resp.data); // 回显上传的图片
                    layer.msg('上传成功');
                } else {
                    layer.msg(resp.msg);
                }
            },
            error: function () {
                layer.msg('上传失败');
            }
        });

        // 表单提交
        form.on('submit(addChannel)', function (data) {
            // 提交数据
            $.ajax({
                url: '/category/add',
                type: 'POST',
                data: JSON.stringify(data.field),
                contentType: 'application/json',
                success: function (res) {
                    if (res.code == 0) {
                        layer.msg('添加成功');
                    } else {
                        layer.msg('添加失败');
                    }
                },
                error: function () {
                    layer.msg('请求失败');
                }
            });
            return false; // 阻止表单跳转
        });
    });
</script>
</body>
</html>